import time
import math
import random

SLEEP = 5

def timestamp():
    return int(time.time())

class Controller:
    def __init__(self, crawl_times=-1, max_api_times=10, limit=20):
        self.offset = 0  # 起始
        self.limit = limit  # 每次下载的数量
        self.stop = False  # 停止控制器
        self.craw_times=crawl_times
        self.max_api_times = max_api_times

    def increase(self, num=None):
        if num is None:
            self.offset += self.limit
        else:
            self.offset += num
        if num < self.limit:
            # the resources is exhousted
            self.to_stop()

    def sleep(self):
        time.sleep(random.randint(10, 20))

    def next_offset(self):
        return self.offset

    def is_end(self):
        return self.stop

    def to_stop(self):
        self.stop = True

    def running_status(self, is_end: bool = False):
        if is_end:
            self.to_stop()
